package com.zjd.association.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分页包装
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@Data
public class PageWrapper {

    /**
     * 当前页码
     */
    private Long pageIndex;

    /**
     * 每页数据量
     */
    private Long pageSize;

    /**
     * 查询的总页数
     */
    private Long pageTotal;

    /**
     * 符合条件的总记录数
     */
    private Long count;

    /**
     * 分页查询包含的结果集
     */
    private List<Map<String, Object>> data = new ArrayList<>();

    public PageWrapper() {
        super();
    }

    public PageWrapper(Long pageIndex, Long pageSize, Long count, List<Map<String, Object>> data) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.count = count;
        this.data = (data != null && data.size() > 0) ? data : this.data;
        if (count > 0) {
            this.pageTotal = (count % pageSize) == 0 ? (count / pageSize) : (count / pageSize + 1);
        } else {
            this.pageTotal = 0L;
        }
    }

    /**
     * 解析保证分页对象
     *
     * @param p 分页对象
     * @return 分页包装
     */
    public static PageWrapper parsePage(Page<Map<String, Object>> p) {
        PageWrapper pageWrapper = new PageWrapper(
                p.getCurrent(), p.getSize(), p.getTotal(), p.getRecords()
        );
        return pageWrapper;
    }

}
